﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;
using Kooboo.Collections;

namespace Kooboo.Commerce.Repositories
{


    [Export(typeof(IShippingRepository))]
    public partial class EntityRepositories : IShippingRepository
    {
      
        public IQueryable<IShipping> Shippings
        {
            get
            {
                return this.DataReaderContext.Shippings;
            }
        }

        public void Post(IShipping shipping)
        {
            Contract.Requires(shipping != null);

            var item = new Shipping()
            {
                Name = shipping.Name
            };

            this.DataWriterContext.Shippings.InsertOnSubmit(item);
            this.DataWriterContext.SubmitChanges();

            shipping.Id = item.Id;

            foreach (var i in shipping.ShippingCostRules)
            {
                var rule = new ShippingCostRule
                {
                    From = i.From,
                    To = i.To,
                    ShippingId = item.Id,
                    Cost = i.Cost
                };


                this.DataWriterContext.ShippingCostRules.InsertOnSubmit(rule);
                this.DataWriterContext.SubmitChanges();
                i.Id = rule.Id;
            }
            
        }

        public void Put(IShipping shipping)
        {
            Contract.Requires(shipping != null);

            var item = this.DataWriterContext.Shippings
                .Where(i => i.Id == shipping.Id)
                .FirstOrDefault();

            if (item == null)
            {
                throw new KeyNotFoundException("Shipping [" + shipping.Name+"] has not been found.");
            }

            item.Name = shipping.Name;

            var rules = this.DataWriterContext.ShippingCostRules.Where(i => i.ShippingId == item.Id);

            var result = rules.Compare(shipping.ShippingCostRules,
                (i1, i2) => i1.Id == i2.Id,
                (i1, i2) =>
                {
                    i1.From = i2.From;
                    i1.To = i2.To;
                    i1.Cost = i2.Cost;
                });

            foreach (var i in result.Deleted)
            {
                this.DataWriterContext.ShippingCostRules.DeleteOnSubmit((ShippingCostRule)i);
            }

          

            this.DataWriterContext.SubmitChanges();

            foreach (var i in result.Added)
            {
                var rule = new ShippingCostRule
                {
                    From = i.From,
                    To = i.To,
                    ShippingId = item.Id,
                    Cost = i.Cost
                };


                this.DataWriterContext.ShippingCostRules.InsertOnSubmit(rule);
                this.DataWriterContext.SubmitChanges();
                i.Id = rule.Id;
            }
        }

        public void Delete(IShipping shipping)
        {
            if (shipping != null)
            {
                var item = this.DataWriterContext.Shippings
                .Where(i => i.Id == shipping.Id)
                .FirstOrDefault();

                if (item != null)
                {
                    this.DataWriterContext.ShippingCostRules.DeleteAllOnSubmit(this.DataWriterContext.ShippingCostRules.Where(i => i.ShippingId == item.Id));
                    this.DataWriterContext.Shippings.DeleteOnSubmit(item);
                    this.DataWriterContext.SubmitChanges();
                }
            }
        }
    }
}
